package com.lsy.weizhi2.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapBaseIndoorMapInfo;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.google.gson.Gson;
import com.lsy.weizhi2.JsonObject.ImageJson;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.adapter.ImageAdapter2;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.config.MyHeight;
import com.lsy.weizhi2.config.MyInfo;
import com.lsy.weizhi2.fragment.FuJingFragment;
import com.lsy.weizhi2.mybase.BaseApp;
import com.lsy.weizhi2.mybase.MyBaseActivity;
import com.lsy.weizhi2.mybase.MyTitleView;
import com.lsy.weizhi2.myutils.AddressCall;
import com.lsy.weizhi2.myutils.ImageAdapterCall;
import com.lsy.weizhi2.myutils.SendPhotoCall;
import com.lsy.weizhi2.picture.PicturePopup;
import com.lsy.weizhi2.picture.PictureUtils;
import com.lsy.weizhi2.popupwindow.SelectPopup;
import com.lsy.weizhi2.third.BaseStripAdapter;
import com.lsy.weizhi2.third.LocationService;
import com.lsy.weizhi2.third.StripListView;
import com.lsy.weizhi2.utils.BitmapUtils;
import com.lsy.weizhi2.utils.MyMethod;
import com.lsy.weizhi2.utils.MyOkHttpUtils;
import com.lsy.weizhi2.utils.RegexUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;

public class SendNote extends MyBaseActivity implements View.OnClickListener {
    private Context context;
    private MyTitleView send_note_title;
    private TextView send_note_buy, send_note_sell, send_note_classify, send_note_time_tv, send_note_send;
    private EditText send_note_price_tv, send_note_phone, show_et, send_note_title_et, send_note_content_et;
    private RadioGroup send_note_rg;
    private RadioButton send_note_rb1, send_note_rb2, send_note_rb3, send_note_rb;
    private GridView send_note_image_gv;
    private ImageAdapter2 imageAdapter;
    private Gson gson;
    private List<Integer> list_id;
    private int xx;
    private double lat,lng;
    private int flag;//flag求或售的标志
    private int priceflag;//计价方式
    public static int first_c, second_c;//第一级，第二级分类
    private int RG_index;//号码可见方式
    private boolean xxx;//附近是否可见
    public static String name;//选择的分类
    private String show;//打赏可见时的价格
    private int isNear;//是否附近可见
    private Handler handler = new Handler();
    private ScrollView scrollView;

    //地图相关
    LocationClient mLocClient;
    public MyLocationListenner myListener = new MyLocationListenner();
    private MyLocationConfiguration.LocationMode mCurrentMode;
    BitmapDescriptor mCurrentMarker;
    MapView mMapView;
    BaiduMap mBaiduMap;
    View mainview;
    StripListView stripListView;
    BaseStripAdapter mFloorListAdapter;
    MapBaseIndoorMapInfo mMapBaseIndoorMapInfo = null;
    boolean isFirstLoc = true; // 是否首次定位
    private LocationService locationService;
    private BitmapDescriptor bitmap1,bitmap2;
    private double latitude,longitude;

    /**
     * 定位SDK监听函数
     */
    public class MyLocationListenner implements BDLocationListener {

        private String lastFloor = null;

        @Override
        public void onReceiveLocation(BDLocation location) {
            // map view 销毁后不在处理新接收的位置
            if (location == null || mMapView == null) {
                return;
            }
            String bid = location.getBuildingID();
            if (bid != null && mMapBaseIndoorMapInfo != null) {
                Log.i("indoor", "bid = " + bid + " mid = " + mMapBaseIndoorMapInfo.getID());
                if (bid.equals(mMapBaseIndoorMapInfo.getID())) {// 校验是否满足室内定位模式开启条件
                    // Log.i("indoor","bid = mMapBaseIndoorMapInfo.getID()");
                    String floor = location.getFloor().toUpperCase();// 楼层
                    Log.i("indoor", "floor = " + floor + " position = " + mFloorListAdapter.getPosition(floor));
                    Log.i("indoor", "radius = " + location.getRadius() + " type = " + location.getNetworkLocationType());

                    boolean needUpdateFloor = true;
                    if (lastFloor == null) {
                        lastFloor = floor;
                    } else {
                        if (lastFloor.equals(floor)) {
                            needUpdateFloor = false;
                        } else {
                            lastFloor = floor;
                        }
                    }
                    if (needUpdateFloor) {// 切换楼层
                        mBaiduMap.switchBaseIndoorMapFloor(floor, mMapBaseIndoorMapInfo.getID());
                        mFloorListAdapter.setSelectedPostion(mFloorListAdapter.getPosition(floor));
                        mFloorListAdapter.notifyDataSetInvalidated();
                    }

                    if (!location.isIndoorLocMode()) {
                        mLocClient.startIndoorMode();// 开启室内定位模式，只有支持室内定位功能的定位SDK版本才能调用该接口
                        Log.i("indoor", "start indoormod");
                    }
                }
            }

            MyLocationData locData = new MyLocationData.Builder().accuracy(location.getRadius())
                    // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(100).latitude(location.getLatitude()).longitude(location.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);
            if (isFirstLoc) {
                isFirstLoc = false;
                LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
                MapStatus.Builder builder = new MapStatus.Builder();
                builder.target(ll).zoom(18.0f);
                mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
            }
        }

        @Override
        public void onConnectHotSpotMessage(String s, int i) {

        }

        public void onReceivePoi(BDLocation poiLocation) {
        }
    }

    private void findMap() {
        mMapView = (MapView) findViewById(R.id.send_note_nearby_mv);
        locationService = BaseApp.locationService;
        mBaiduMap = mMapView.getMap();
        //隐藏缩放控件
        mMapView.showZoomControls(false);
        //隐藏百度logo
        mMapView.removeViewAt(1);
        // 开启定位图层
        mBaiduMap.setMyLocationEnabled(true);
        // 开启室内图
        mBaiduMap.setIndoorEnable(true);
        mBaiduMap.clear();
        // 定位初始化
        // LatLng latLng=new LatLng();
        // MarkerOptions options=new MarkerOptions().icon()
        mLocClient = new LocationClient(BaseApp.context);
        mLocClient.registerLocationListener(myListener);
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(3000);
        mLocClient.setLocOption(option);
        mLocClient.start();
        stripListView = new StripListView(context);
        mFloorListAdapter = new BaseStripAdapter(context);
        bitmap1 = BitmapDescriptorFactory.fromResource(R.mipmap.nearby_self);
        bitmap2 = BitmapDescriptorFactory.fromResource(R.mipmap.touxiang);
        //地图点击事件监听
        mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                mBaiduMap.clear();
                mCurrentMode = MyLocationConfiguration.LocationMode.NORMAL;
                mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(mCurrentMode, true,
                        mCurrentMarker));
                latitude=latLng.latitude;
                longitude=latLng.longitude;
                MarkerOptions markerOptions = new MarkerOptions()
                        .position(latLng)
                        .icon(bitmap1);
                mBaiduMap.addOverlay(markerOptions);
            }

            @Override
            public boolean onMapPoiClick(MapPoi mapPoi) {
                return false;
            }
        });
    }

    private void LoadMap() {
        //根据位置加载图像
        MarkerOptions markerOptions = new MarkerOptions()
                .position(new LatLng(MyInfo.Latitude, MyInfo.Longitude))
                .icon(bitmap1).zIndex(3);

        mBaiduMap.addOverlay(markerOptions);

        mBaiduMap.setOnBaseIndoorMapListener(new BaiduMap.OnBaseIndoorMapListener() {
            @Override
            public void onBaseIndoorMapMode(boolean b, MapBaseIndoorMapInfo mapBaseIndoorMapInfo) {
                if (b == false || mapBaseIndoorMapInfo == null) {
                    stripListView.setVisibility(View.INVISIBLE);

                    return;
                }
                mFloorListAdapter.setmFloorList(mapBaseIndoorMapInfo.getFloors());
                stripListView.setVisibility(View.VISIBLE);
                stripListView.setStripAdapter(mFloorListAdapter);
                mMapBaseIndoorMapInfo = mapBaseIndoorMapInfo;
            }
        });

        mMapView.onResume();
        //获取locationservice实例，建议应用中只初始化1个location实例，然后使用，可以参考其他示例的activity，都是通过此种方式获取locationservice实例的
        locationService.registerListener(MyInfo.mListener);
        locationService.setLocationOption(locationService.getDefaultLocationClientOption());
        locationService.start();
    }

    private void UnBundMap() {
        mMapView.onPause();
        if (locationService != null) {
            Log.i("HomePage", "开始关闭");
            locationService.unregisterListener(MyInfo.mListener); //注销掉监听
            locationService.stop(); //停止定位服务
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_note);
        context = SendNote.this;
        Init();
        findMap();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        xx = 0;
        ViewTreeObserver vto = send_note_title.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (xx < 1) {
                    xx++;
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(send_note_title.getLayoutParams());
                            params.height = MyHeight.ACTIONBAR + MyHeight.MyTitleHeight;
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                                send_note_title.setLayoutParams(params);
                            }
                        }
                    });
                }
            }
        });
        Log.i("SendNote","onResume");
        send_note_classify.setText(name);
        if (getIntent().getStringExtra("name") != null) {
            first_c = Integer.parseInt(getIntent().getStringExtra("first"));
            second_c = Integer.parseInt(getIntent().getStringExtra("second"));
            name = getIntent().getStringExtra("name");
            send_note_classify.setText(name);
        }

        Log.i("SendNote","name==="+name+"\nfirst=="+first_c+"\nsecond==="+second_c);
        LoadMap();
        View view = mMapView.getChildAt(0);
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_UP){
                    scrollView.requestDisallowInterceptTouchEvent(false);
                }else{
                    scrollView.requestDisallowInterceptTouchEvent(true);
                }
                return false;
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.i("SendNote","onPause");
        xx = 0;
        UnBundMap();
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        send_note_price_tv.setText(savedInstanceState.getString("price"));
        send_note_time_tv.setText(savedInstanceState.getString("method"));
        send_note_phone.setText(savedInstanceState.getString("phone"));
        send_note_title_et.setText(savedInstanceState.getString("title"));
        send_note_content_et.setText(savedInstanceState.getString("content"));
        Log.i("SendNote","onRestoreInstanceState");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        String price = send_note_price_tv.getText().toString();//获取价格
        String method = send_note_time_tv.getText().toString();//获取计价方式
        String phone = send_note_phone.getText().toString();//获取电话号码
        String title = send_note_title_et.getText().toString();//获取主题
        String content = send_note_content_et.getText().toString();//获取内容
        outState.putString("price",price);
        outState.putString("method",method);
        outState.putString("phone",phone);
        outState.putString("title",title);
        outState.putString("content",content);
        Log.i("SendNote","onSaveInstanceState");
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    private void Init() {
        gson = new Gson();
        list_id = new ArrayList<>();
        scrollView= (ScrollView) findViewById(R.id.scrollView);
        send_note_title = (MyTitleView) findViewById(R.id.send_note_title);
        send_note_buy = (TextView) findViewById(R.id.send_note_buy);
        send_note_sell = (TextView) findViewById(R.id.send_note_sell);
        send_note_classify = (TextView) findViewById(R.id.send_note_classify);
        send_note_time_tv = (TextView) findViewById(R.id.send_note_time_tv);
        send_note_send = (TextView) findViewById(R.id.send_note_send);
        send_note_price_tv = (EditText) findViewById(R.id.send_note_price_tv);
        send_note_phone = (EditText) findViewById(R.id.send_note_phone);
        show_et = (EditText) findViewById(R.id.show_et);
        send_note_title_et = (EditText) findViewById(R.id.send_note_title_et);
        send_note_content_et = (EditText) findViewById(R.id.send_note_content_et);
        send_note_rg = (RadioGroup) findViewById(R.id.send_note_rg);
        send_note_rb1 = (RadioButton) findViewById(R.id.send_note_rb1);
        send_note_rb2 = (RadioButton) findViewById(R.id.send_note_rb2);
        send_note_rb3 = (RadioButton) findViewById(R.id.send_note_rb3);
        send_note_rb = (RadioButton) findViewById(R.id.send_note_rb);
        send_note_image_gv = (GridView) findViewById(R.id.send_note_image_gv);
        imageAdapter = new ImageAdapter2(context, null);
        send_note_image_gv.setAdapter(imageAdapter);
        send_note_phone.setEnabled(false);
        send_note_phone.setText(MyInfo.Phone);
        send_note_buy.setOnClickListener(this);
        send_note_sell.setOnClickListener(this);
        send_note_classify.setOnClickListener(this);
        send_note_time_tv.setOnClickListener(this);
        send_note_rb.setOnClickListener(this);
        send_note_send.setOnClickListener(this);
        send_note_rb1.setChecked(true);
        send_note_rb.setChecked(true);
        send_note_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i == R.id.send_note_rb1) {
                    RG_index = 0;
                } else if (i == R.id.send_note_rb2) {
                    RG_index = 1;
                } else if (i == R.id.send_note_rb3) {
                    RG_index = 2;
                    show = show_et.getText().toString();
                }
            }
        });
        imageAdapter.setImageAdapterCall(new ImageAdapterCall() {
            @Override
            public void onClick(View view, final int x) {
                new PicturePopup().init(context, new SendPhotoCall() {
                    @Override
                    public void Request(String message) {
                        if (message != null) {
                            try {
                                ImageJson imageJson = gson.fromJson(message, ImageJson.class);
                                int id = imageJson.getData().getId();
                                list_id.add(id);
                                imageAdapter.UpData(list_id);
                            } catch (Exception e) {
                                Log.i("SendNote", "message==image=" + message);
                            }
                        }
                    }
                });
            }

            @Override
            public void onDetele(View view, int x) {
                list_id.remove(x);
                imageAdapter.UpData(list_id);
                //要发送到服务器删
            }
        });
        send_note_price_tv.setOnClickListener(this);
        show_et.setOnClickListener(this);
        send_note_title_et.setOnClickListener(this);
        send_note_content_et.setOnClickListener(this);
    }

    private void setBuy() {
        flag = 0;
        send_note_buy.setTextColor(getResources().getColor(R.color.white));
        send_note_buy.setBackgroundResource(R.mipmap.blue);
        send_note_sell.setTextColor(getResources().getColor(R.color.classify_text));
        send_note_sell.setBackgroundResource(R.mipmap.fillet_7);
    }

    private void setSell() {
        flag = 1;
        send_note_sell.setTextColor(getResources().getColor(R.color.white));
        send_note_sell.setBackgroundResource(R.mipmap.blue);
        send_note_buy.setTextColor(getResources().getColor(R.color.classify_text));
        send_note_buy.setBackgroundResource(R.mipmap.fillet_7);
    }

    /**
     * 发帖子
     */
    private void getAllInfo() {
        String photo=null;
        if(list_id!=null&&list_id.size()!=0){
            StringBuffer sb=new StringBuffer();
            for (int i = 0; i < list_id.size(); i++) {
                if(i==list_id.size()-1){
                    sb.append(list_id.get(i));
                }else{
                    sb.append(list_id.get(i)+",");
                }
            }
            photo=sb.toString();
        }
        String price = send_note_price_tv.getText().toString();//获取价格
        String method = send_note_time_tv.getText().toString();//获取计价方式
        switch (method){
            case "次":
                priceflag = 0;
                break;
            case "时":
                priceflag = 1;
                break;
            case "天":
                priceflag = 2;
                break;
            case "周":
                priceflag = 3;
                break;
            case "月":
                priceflag = 4;
                break;
            case "年":
                priceflag = 5;
                break;
        }
        String phone = send_note_phone.getText().toString();//获取电话号码
        String title = send_note_title_et.getText().toString();//获取主题
        String content = send_note_content_et.getText().toString();//获取内容
        send_note_rb.isChecked();//获取是否附近可见
        if (title == null || title.length() == 0) {
            MyMethod.ShowDialog(context, "标题不能为空");
            return;
        }
        if (content == null || content.length() == 0) {
            MyMethod.ShowDialog(context, "内容不能为空");
            return;
        }
        if (!RegexUtils.checkMobile(phone)) {
            MyMethod.ShowDialog(context, "请输入正确手机号码");
            return;
        }
        if(price==null||price.length()==0){
            MyMethod.ShowDialog(context, "价格不能为空");
            return;
        }
        if(latitude==0&&MyInfo.Latitude==0){
            MyMethod.ShowDialog(context, "请在地图上选择位置");
            return;
        }
        if(send_note_rb.isChecked()){
            isNear=1;
        }else{
            isNear=0;
        }
        Map<String, String> map = new HashMap<>();
        map.put("title", title);//标题
        map.put("content", content);//内容
        map.put("phone", phone);//电话号码
        map.put("photo",photo);//图片地址
        map.put("phoneType", RG_index + "");//电话号码方式--保密，公开，打赏可见
        map.put("phoneReward", show);//打赏金额--可见电话号码
        map.put("type", flag + "");//求 或 售
        map.put("priceMethod", priceflag + "");//取费方式--0 次，1 时，2 天
        map.put("price", price);//价格
        map.put("city", MyInfo.UserCity);//城市
        map.put("firstCategoryId", first_c + "");//一级分类
        map.put("secondCategoryId", second_c + "");//二级分类
        if(latitude!=0){
            map.put("latitude", latitude + "");//纬度
            map.put("longitude", longitude + "");//经度
        }else{
            map.put("latitude", MyInfo.Latitude + "");//纬度
            map.put("longitude", MyInfo.Longitude + "");//经度
        }
        map.put("isNear",isNear+"");//是否附近可见
        MyOkHttpUtils.getInstance(context).doPost(Constants.ORDERLIST, map)
                .excute(new MyOkHttpUtils.OKCallBack() {
                    @Override
                    public void onFailure(String message) {
                        MyMethod.ShowDialog(context,"发送失败:\n"+message);
                        Log.i("SendNote", "发送失败====" + message);
                    }

                    @Override
                    public void onResponse(String message) {
                        if (message != null) {
                            try {
                                JSONObject jsonObject = new JSONObject(message);
                                boolean result = jsonObject.getBoolean("result");
                                if (result) {
                                    Log.i("SendNote", "发送成功");
                                    MyMethod.ShowDiaLogFinish(context,"发送成功");
                                } else {
                                    MyMethod.ShowDialog(context,"发送失败:\n"+message);
                                    Log.i("SendNote", "发送失败====" + message);
                                }


                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.send_note_sell:
                setSell();
                break;
            case R.id.send_note_buy:
                setBuy();
                break;
            case R.id.send_note_classify:
                CustomPage.Tag = 1;
                MyMethod.toActivity(context, CustomPage.class);
                break;
            case R.id.send_note_time_tv:
                SelectPopup popup = new SelectPopup(new AddressCall() {
                    @Override
                    public void Select(String p, String c, String d, String f) {
                        switch (f) {
                            case "address":
                                send_note_time_tv.setText(p);
                                break;
                        }
                    }
                });
                popup.setShowCount(1);
                popup.setFlag("address");
                String[] strs = {"时", "天", "周", "月", "年", "次"};
                popup.setStrs(strs);
                popup.init(context);
                break;
            case R.id.send_note_rb:
                Log.i("SendNote", "附近可见" + xxx);
                if (!xxx) {
                    send_note_rb.setChecked(false);
                } else {
                    send_note_rb.setChecked(true);
                }
                xxx = !send_note_rb.isChecked();
                break;
            case R.id.send_note_send://发布帖子
                getAllInfo();
                break;
            case R.id.send_note_price_tv://光标放最后
                send_note_price_tv.setSelection(send_note_price_tv.getText().length());
                break;
            case R.id.show_et://光标放最后
                show_et.setSelection(show_et.getText().length());
                break;
            case R.id.send_note_title_et://光标放最后
                send_note_title_et.setSelection(send_note_title_et.getText().length());
                break;
            case R.id.send_note_content_et://光标放最后
                send_note_content_et.setSelection(send_note_content_et.getText().length());
                break;
        }
    }
    //请求权限的返回结果
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PictureUtils.CAMERA) {
            for (int index = 0; index < permissions.length; index++) {
                switch (permissions[index]) {
                    case Manifest.permission.CAMERA:
                        if (grantResults[index] == PackageManager.PERMISSION_GRANTED) {
                            /**用户已经受权*/
                            startActivityForResult(
                                    new Intent(MediaStore.ACTION_IMAGE_CAPTURE), PictureUtils.CAMERA);
                        } else if (grantResults[index] == PackageManager.PERMISSION_DENIED) {
                            /**用户拒绝了权限*/
                            MyMethod.ShowDialog(context,"应用没有拍照权限，请授权！");
                        } else {
                            MyMethod.ShowDialog(context,"应用没有拍照权限，请授权！");
                        }
                        break;
                }
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.i("SendNote", "requestCode==2==" + requestCode);
        Log.i("SendNote", "file=1==" + BaseApp.file1.length());
        if (requestCode == PictureUtils.CAMERA) {
            Bitmap bitmap = data.getParcelableExtra("data");
            int count = bitmap.getByteCount();
            Log.i("SendNote", "count====" + count);

            if(count>=1048576){
                bitmap= BitmapUtils.getImage(bitmap);
            }
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(BaseApp.file1);
                bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }finally {
                if(fos!=null){
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            UpImage(BaseApp.file1);

        }
    }

    private void UpImage(File file) {
        MyOkHttpUtils.getInstance(context).upLoadImage(Constants.UP_USER_PICTURE, file).excute(new MyOkHttpUtils.OKCallBack() {

            @Override
            public void onFailure(String message) {
                Log.i("SendNote", "message=image=Fail=" + message);
                MyMethod.ShowDialog(context,"图片发送失败");
            }

            @Override
            public void onResponse(String message) {
                Log.i("SendNote", "message=image=Success=" + message);
                try {
                    JSONObject jsonObject=new JSONObject(message);
                    boolean result=jsonObject.getBoolean("result");
                    if(result){
                        ImageJson imageJson = gson.fromJson(message, ImageJson.class);
                        int id = imageJson.getData().getId();
                        list_id.add(id);
                        imageAdapter.UpData(list_id);
                    }else{
                        String msg=jsonObject.getString("message");
                        MyMethod.ShowDialog(context,msg);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    @Override
    protected void onDestroy() {
        // 退出时销毁定位
        mLocClient.stop();
        // 关闭定位图层
        mBaiduMap.setMyLocationEnabled(false);
        mMapView.onDestroy();
        mMapView = null;
        super.onDestroy();
    }
}